package com.store.orm.io;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.store.orm.config.MongoConfig;
import com.store.orm.config.MySQLConfig;
import com.store.orm.config.Pool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * JSON 配置文件解析类
 * 主要是将 JSON 配置文件解析为 Map，方便调用
 *
 * @author 宫静雨
 * @version 1.0
 */
public class ParseJson {
    private static final Logger logger = LoggerFactory.getLogger(ParseJson.class);

    public Map<String, MongoConfig> parseMongo(File file) {
        Map<String, MongoConfig> map = new HashMap<>();
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> jsonMap = null;
        try {
            jsonMap = mapper.readValue(file, new TypeReference<Map<String, Object>>() {
            });
        } catch (IOException e) {
            logger.error("解析JSON配置文件时发生错误: {}", e.getMessage());
            throw new RuntimeException();
        }
        // 遍历解析 JSON 文件后的 Map 数据，将其映射到 MySQLConfig 对象实例中
        jsonMap.forEach((k1, v1) -> {
            LinkedHashMap<String, Object> values = (LinkedHashMap<String, Object>) v1;
            MongoConfig config = new MongoConfig();
            values.forEach((k2, v2) -> {
                switch (k2) {
                    case "host":
                        config.setHost((String) v2);
                        break;
                    case "port":
                        config.setPort((Integer) v2);
                        break;
                    case "db":
                        config.setDb((String) v2);
                        break;
                }
                if (v2 instanceof LinkedHashMap) {
                    LinkedHashMap<String, Integer> linkedHashMap = (LinkedHashMap<String, Integer>) v2;
                    Pool pool = new Pool();
                    linkedHashMap.forEach((k3, v3) -> {
                        switch (k3) {
                            case "init":
                                pool.setInit(v3);
                                break;
                            case "max":
                                pool.setMax(v3);
                                break;
                        }
                    });
                    config.setPool(pool);
                }
            });
            map.put(k1, config); //处理 k1，去掉 mysql_ 前缀
        });
        return map;
    }

    public Map<String, MySQLConfig> parseMySQL(File file) {
        Map<String, MySQLConfig> map = new HashMap<>();
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> jsonMap = null;
        try {
            jsonMap = mapper.readValue(file, new TypeReference<Map<String, Object>>() {
            });
        } catch (IOException e) {
            logger.error("解析JSON配置文件时发生错误: {}", e.getMessage());
            throw new RuntimeException();
        }
        // 遍历解析 JSON 文件后的 Map 数据，将其映射到 MySQLConfig 对象实例中
        jsonMap.forEach((k1, v1) -> {
            LinkedHashMap<String, Object> values = (LinkedHashMap<String, Object>) v1;
            MySQLConfig config = new MySQLConfig();
            values.forEach((k2, v2) -> {
                switch (k2) {
                    case "driver":
                        config.setDriver((String) v2);
                        break;
                    case "url":
                        config.setUrl((String) v2);
                        break;
                    case "username":
                        config.setUsername((String) v2);
                        break;
                    case "password":
                        config.setPassword((String) v2);
                        break;
                    case "maxBatch":
                        config.setMaxBatch((Integer) v2);
                        break;
                    case "logged":
                        config.setLogged((Boolean) v2);
                        break;
                    case "camelCase":
                        config.setCamelCase((Boolean) v2);
                        break;
                    case "batchLog":
                        config.setBatchLog((Boolean) v2);
                        break;
                    case "truncate":
                        config.setTruncate((Boolean) v2);
                        break;
                    case "updateAll":
                        config.setUpdateAll((Boolean) v2);
                        break;
                }
                if (v2 instanceof LinkedHashMap) {
                    LinkedHashMap<String, Integer> linkedHashMap = (LinkedHashMap<String, Integer>) v2;
                    Pool pool = new Pool();
                    linkedHashMap.forEach((k3, v3) -> {
                        switch (k3) {
                            case "init":
                                pool.setInit(v3);
                                break;
                            case "max":
                                pool.setMax(v3);
                                break;
                        }
                    });
                    config.setPool(pool);
                }
            });
            map.put(k1, config); //处理 k1，去掉 mysql_ 前缀
        });
        return map;
    }
}